package com.dd.cloud.user.service.chknowl.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.chknowl.ChknowlChVideo;
import com.dd.cloud.user.entity.chknowl.ChnowlVideoRecord;
import com.dd.cloud.user.entity.product.ProductUseRecord;
import com.dd.cloud.user.mapper.chknowl.ChnowlVideoRecordMapper;
import com.dd.cloud.user.req.chknowl.ChnowlVideoReq;
import com.dd.cloud.user.service.chknowl.IChknowlChVideoService;
import com.dd.cloud.user.service.chknowl.IChnowlVideoRecordService;
import com.dd.cloud.user.service.product.IProductUseRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 观看视频记录 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-11-03
 */
@Service
public class ChnowlVideoRecordServiceImpl extends ServiceImpl<ChnowlVideoRecordMapper, ChnowlVideoRecord> implements IChnowlVideoRecordService {
    @Autowired
    private IProductUseRecordService productUseRecordService;
    @Autowired
    private IChknowlChVideoService chknowlChVideoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addVideoRecord(ChnowlVideoReq chkonwlVideoReq) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Integer videoId = chkonwlVideoReq.getVideoId();
        LambdaQueryWrapper<ChnowlVideoRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChnowlVideoRecord::getVideoId, videoId).eq(ChnowlVideoRecord::getStatus, 1).eq(ChnowlVideoRecord::getDoctorId, loginInfo.getId()).eq(ChnowlVideoRecord::getIsLast, 1);
        List<ChnowlVideoRecord> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(item -> {
                item.setIsLast(0);
            });
            this.updateBatchById(list);
        }
        LambdaQueryWrapper<ChnowlVideoRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChnowlVideoRecord::getDoctorId, loginInfo.getId()).eq(ChnowlVideoRecord::getVideoId, videoId).eq(ChnowlVideoRecord::getChapterId, chkonwlVideoReq.getChapterId()).eq(ChnowlVideoRecord::getStatus, 1);
        ChnowlVideoRecord chnowlVideoRecord = this.getOne(queryWrapper);
        if (chnowlVideoRecord != null) {
            chnowlVideoRecord.setTime(chkonwlVideoReq.getTime()).setIsLast(1);
            this.updateById(chnowlVideoRecord);
        } else {
            ChnowlVideoRecord newChnowlVideoRecord = new ChnowlVideoRecord();
            BeanUtils.copyProperties(chkonwlVideoReq, newChnowlVideoRecord);
            newChnowlVideoRecord.setDoctorId(loginInfo.getId()).setCreateDate(LocalDateTime.now()).setStatus(1).setIsLast(1);
            this.save(newChnowlVideoRecord);
        }
        ChknowlChVideo video = chknowlChVideoService.queryVideoById(videoId);
        ProductUseRecord record = new ProductUseRecord();
        record.setDoctorId(loginInfo.getId()).setDoctorName(loginInfo.getName()).setKcId(videoId).setKcName(video.getName()).setKcTime(chkonwlVideoReq.getTime()).setType(2);
        productUseRecordService.createProductUseRecord(record);
    }

    @Override
    public ChnowlVideoRecord queryVideoRecord(Integer chapterId) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        LambdaQueryWrapper<ChnowlVideoRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChnowlVideoRecord::getChapterId, chapterId).eq(ChnowlVideoRecord::getDoctorId, loginInfo.getId()).eq(ChnowlVideoRecord::getStatus, 1);
        return this.getOne(wrapper);
    }
}
